package com.juguo.charginganimation.View.gallery;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.juguo.charginganimation.Bean.PhotoBean;
import com.juguo.charginganimation.Data.remote.RetrofitManager;
import com.juguo.charginganimation.MainApplication;
import com.juguo.charginganimation.R;
import com.juguo.charginganimation.View.BeiSaiView.HwChargingView;
import com.juguo.charginganimation.repository.PixabayService;
import com.juguo.charginganimation.utils.RxUtils;
import com.juguo.charginganimation.utils.ToastUtil;
import com.juguo.charginganimation.utils.UITools;
import com.juguo.charginganimation.utils.VirtualBarUtil;

import java.io.File;
import java.util.zip.Deflater;

import io.reactivex.disposables.CompositeDisposable;
import io.supercharge.shimmerlayout.ShimmerLayout;
import pl.droidsonroids.gif.GifImageView;

public class GalleryDetailYyds extends AppCompatActivity {

    private static final String TAG = "GalleryDetailYyds";

    private ShimmerLayout shimmerLayout;
    private GifImageView photoView;

    private HwChargingView mHwChargingView;

    private TextView mProgressTv;
    private SeekBar mProgressSb;
    static Activity activity;
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /** 判断是否需要隐藏底部的虚拟按键 */
        if (VirtualBarUtil.hasNavBar(this)) {
            VirtualBarUtil.hideBottomUIMenu(this);
        }
        setContentView(R.layout.activity_gallery_yyds);
        shimmerLayout = findViewById(R.id.shimerDetialIMG);
        photoView = findViewById(R.id.photoViews);
        mHwChargingView = findViewById(R.id.hw_charging);
        UITools.makeStatusBarTransparent(this);
        activity = this;
        mProgressTv = findViewById(R.id.seek_tv);
        mProgressSb = findViewById(R.id.seekBar);

        /** 手机当前电量变化的广播监测 */
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_BATTERY_LOW);
        filter.addAction(Intent.ACTION_BATTERY_OKAY);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        registerReceiver(mBroadcastReceiver, filter);
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean isBatteryCharging = false;
            String action = intent.getAction();
            switch (action) {
                case Intent.ACTION_BATTERY_CHANGED:
                    //电量发生改变。
//                    ToastUtil.showToast(GalleryDetailYyds.this, "电量发生改变");
                    // 获取充电状态
                    int batteryChargeState = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                    // 判定是否是 AC 交流电
                    boolean isAc = batteryChargeState == BatteryManager.BATTERY_PLUGGED_AC;
                    // 判断是否是 USB 充电
                    boolean isUsb = batteryChargeState == BatteryManager.BATTERY_PLUGGED_USB;
                    // 判断是否是 无线充电
                    boolean isWireless = batteryChargeState == BatteryManager.BATTERY_PLUGGED_WIRELESS;
                    // 如何上述任意一种为 true , 说明当前正在充电
                    isBatteryCharging = isAc || isUsb || isWireless;
                    if (isBatteryCharging) {
                        //剩余电量。
                        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                        //电量最大值。
                        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                        //电量百分比。
                        float batteryPct = level / (float) scale;
                        Log.e(TAG, "充电," + level + "-" + batteryPct + "-" + scale
                                +"AC交流电"+isAc + "USB" + isUsb + "无线充电" + isWireless);
                        mHwChargingView.setProgress(level);
                        mProgressTv.setText(batteryPct + "%");
                        mProgressSb.setProgress(level);
                    }
                    break;
//                case Intent.ACTION_POWER_CONNECTED:
////                    ToastUtil.showToast(GalleryDetailYyds.this, "电源接通,");
//                    break;

                case Intent.ACTION_POWER_DISCONNECTED:
//                    ToastUtil.showToast(GalleryDetailYyds.this, "电源断开3333333333333333333333333333,");
//                    activity.finish();
                    moveTaskToBack(true);  //回退任务栈
                    break;
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        //shimmerLayout动画初始化
        shimmerLayout.setShimmerColor(0X55FFFFFF);
        shimmerLayout.setShimmerAngle(0);
        shimmerLayout.startShimmerAnimation();
        initGalleryDetailView();
    }

    /**
     * 初始化充电界面视图
     */
    private void initGalleryDetailView() {
        SharedPreferences sp = MainApplication.getsInstance().getSharedPreferences("ImageYyds", Context.MODE_PRIVATE);
        String coverImgUrl = sp.getString("coverImgUrl", "");
        //传入图片URL，Glide初始化
        Glide.with(getApplicationContext())
                //加载的URL
                .load(coverImgUrl)
                //占位图片初始化
//                .placeholder(R.drawable.ic_crop_original_black_24dp)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }
                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (shimmerLayout != null) {     //加载成功则停止动画
                            shimmerLayout.stopShimmerAnimation();
                        }
                        return false;
                    }
                })
                .into(photoView);    //装载图片
    }

    /**
     * 重写onBackPressed，用于配对进入动画
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        /** 注销手机电量变化的监测广播 */
        if (mBroadcastReceiver != null) {
            unregisterReceiver(mBroadcastReceiver);
        }
    }
}
